<template>
    <div>  
        <header class="classification_t header_fixed" id="title">
          <div class="classification_t_l" @click="goPrev">
                <img src="@/assets/images/return.png" width="30%">
          </div>
          <div class="classification_t_r">地图定位</div>
          <div style="float: right;text-align: right;padding-right: 15px; width: 30%;line-height: 55px;" @click='openSearchplace'><img src="@/assets/images/search_icon.png" alt="" width="20%" style="display: inline;"></div>
        </header>
        <div class="amap-wrapper" id="container"></div>
       <!--  <div class="header_input_box">
            <input type="search" placeholder="请输入搜索内容"><span><img src="@/assets/images/search_icon.png" alt="" width="40%"></span>
        </div>     -->
      <ul style="position: fixed;overflow-y: scroll;" class="position_lists">
        <!-- <li>ufhwihfoiwjofj wfojw l</li> -->
        <li style="width: 100%" v-for="item of poilist" @click="choseArea(item)">
            <div class="position_lists_l"><img src="@/assets/images/location_blue_icon.png" alt="" width="30" style="display: inline;vertical-align: middle;padding-right: 10px"></div>
            <div class="position_lists_r">
                <p>{{item.name}}</p>
                <span>{{item.address}}</span>
            </div>
        </li>
      </ul>
    </div>
</template>
<script>
// import TopWithBack from './../common/TopWithBack'
// import { loadAdress, setDefaultAdress, delAdress } from '@/api/allapis'
import { Message, MessageBox, Loading } from 'element-ui'
import AMap from 'AMap'
import AMapUI from 'AMapUI'
export default{
  name: 'Addadress',
  data(){
      return{
          param:{},
          poilist:'',
          adcode:''
      }
  },
  // components:{
  //     TopWithBack
  // },
  methods:{
    //地图初始化
    init(){
      const self = this
      AMapUI.loadUI(['misc/PositionPicker'], function(PositionPicker) {

        var map = new AMap.Map('container', {
            zoom: 16,
            scrollWheel: false,
            resizeEnable: true
        })
        map.plugin(['AMap.Geolocation', 'AMap.ToolBar'], function () {
          let geolocation = new AMap.Geolocation({
            enableHighAccuracy: true, // 是否使用高精度定位，默认:true
            timeout: 10000 // 超过10秒后停止定位，默认：无穷大
            // buttonOffset: new AMap.Pixel(10, 20), // 定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            // zoomToAccuracy: true, // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            // buttonPosition:'RB'
          })
          map.addControl(geolocation)
          map.addControl(new AMap.ToolBar())
          geolocation.getCurrentPosition(function(status,result){
            if(status=='complete'){
                var positionPicker = new PositionPicker({
                    mode: 'dragMap',
                    map: map,
                    position: result.position
                });

                positionPicker.on('success', function(positionResult) {
                      // console.log(JSON.stringify(positionResult))
                      self.poilist = positionResult.regeocode.pois
                      self.adcode = positionResult.regeocode.addressComponent.adcode
                      // console.log(self.adcode)
                });
                positionPicker.on('fail', function(positionResult) {
                });
                positionPicker.start();
            }
          }) // 执行定位功能
        })          
      });
    },
    //返回上一页
    goPrev(){
      // console.log(JSON.stringify(this.param))
      // this.param.adcode = this.adcode
      if(this.$route.query.gotomap=='1'){
        this.$router.replace({
          name:'UpdateAdress',
          params:{
            resonseparam:this.param//回传给修改地址页面的数据
          }
        })//gotomap为1时表示是从修改地址页面打开的map
      }else{
        this.$router.replace({
          name:'Addadress',
          params:{
            resonseparam:this.param//回传给添加地址页面的数据
          }
        })
      }    
    },
    //打开搜索页面（未完成）
    openSearchplace(){

    },
    //回传到我的地址页面
    choseArea(item){
      // console.log(JSON.stringify(item))
      // console.log(this.adcode)      
      if(this.$route.query.gotomap=='1'){
        this.param.adcode = this.adcode
        this.param.address = item.name
        this.$router.replace({
          name:'UpdateAdress',
          params:{
            resonseparam:this.param//回传给修改地址页面的数据
          }
        })
      }else{
        this.$router.replace({
          name:'Addadress',
          params:{
            item:item,
            adcode:this.adcode,
            resonseparam:this.param//回传给添加地址页面的数据
          }
        })
      }  
    },
  },
  created(){
      this.init()
      // console.log(this.$route.params.param)   
      this.param = this.$route.params.param//接受从添加地址传过来的数据和从修改地址
  },
}
</script>
<style>
.amap-wrapper{
  height: 300px;
  margin-top: 50px
}
</style>


